/*
package cn.JHai.rbac.filters;

import com.google.common.base.Charsets;
import com.google.common.base.Strings;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import com.netflix.zuul.http.HttpServletRequestWrapper;
import com.netflix.zuul.http.ServletInputStreamWrapper;
import com.tensquare.encrypt.rsa.RsaKeys;
import com.tensquare.encrypt.service.RsaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.util.StreamUtils;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


@Component
public class RSARequestFilter extends ZuulFilter{


    @Autowired
    private RsaService rsaService;
    @Override
    public String filterType() {
        //过滤器在什么时候执行，解密操作需要在转发后执行
        return FilterConstants.PRE_TYPE;
    }

    @Override
    public int filterOrder() {
        //设置过滤器的执行顺序
        return FilterConstants.PRE_DECORATION_FILTER_ORDER+1;
    }

    @Override
    public boolean shouldFilter() {
        //是否使用这个过滤器

        return true;
    }

    @Override
    public Object run() throws ZuulException {
        //过滤器执行的逻辑

        System.out.println("过滤器执行了");
        //1.get request and response RequestContext
        //import com.netflix.zuul.context.RequestContext;
        //
        RequestContext ctx = RequestContext.getCurrentContext();
        HttpServletRequest request = ctx.getRequest();
        HttpServletResponse response = ctx.getResponse();

        //声明存放加密后的数据变量
        String requestData = null;
        String decryptData = null;

        try {
            ServletInputStream inputStream = request.getInputStream();
            requestData = StreamUtils.copyToString(inputStream, Charsets.UTF_8);
            System.out.println(requestData);
            if(!Strings.isNullOrEmpty(requestData)){
                decryptData = rsaService.RSADecryptDataPEM(requestData, RsaKeys.getServerPrvKeyPkcs8());
                System.out.println(decryptData);

                //传输
                if(!Strings.isNullOrEmpty(decryptData)) {
                    //转化为字节流
                    final byte[] reqBodyBytes = decryptData.getBytes();
                    //这里比较难
                    ctx.setRequest(new HttpServletRequestWrapper(request){

                        @Override
                        public ServletInputStream getInputStream() throws IOException {
                            return new ServletInputStreamWrapper(reqBodyBytes);
                        }

                        @Override
                        public int getContentLength() {
                            return reqBodyBytes.length;
                        }

                        @Override
                        public long getContentLengthLong() {
                            return reqBodyBytes.length;
                        }

                    });

                }
                System.out.println("转发request");
                // 设置request请求头中的Content-Type为application/json，否则api接口模块需要进行url转码操作
                ctx.addZuulRequestHeader("Content-Type", String.valueOf(MediaType.APPLICATION_JSON) + ";charset=UTF-8");

            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        //通过request获取inputStream

        //从inputsteam 中获得加密后的数据

        return null;
    }
}
*/
